function varargout = atoms_2_gui(varargin)
% ATOMS_2_GUI MATLAB code for atoms_2_gui.fig
%      ATOMS_2_GUI, by itself, creates a new ATOMS_2_GUI or raises the existing
%      singleton*.
%
%      H = ATOMS_2_GUI returns the handle to a new ATOMS_2_GUI or the handle to
%      the existing singleton*.
%
%      ATOMS_2_GUI('CALLBACK',hObject,eventData,handles,...) calls the local
%      function named CALLBACK in ATOMS_2_GUI.M with the given input arguments.
%
%      ATOMS_2_GUI('Property','Value',...) creates a new ATOMS_2_GUI or raises the
%      existing singleton*.  Starting from the left, property value pairs are
%      applied to the GUI before atoms_2_gui_OpeningFcn gets called.  An
%      unrecognized property name or invalid value makes property application
%      stop.  All inputs are passed to atoms_2_gui_OpeningFcn via varargin.
%
%      *See GUI Options on GUIDE's Tools menu.  Choose "GUI allows only one
%      instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES

% Edit the above text to modify the response to help atoms_2_gui

% Last Modified by GUIDE v2.5 07-Feb-2014 12:13:34

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
    'gui_Singleton',  gui_Singleton, ...
    'gui_OpeningFcn', @atoms_2_gui_OpeningFcn, ...
    'gui_OutputFcn',  @atoms_2_gui_OutputFcn, ...
    'gui_LayoutFcn',  [] , ...
    'gui_Callback',   []);
if nargin && ischar(varargin{1})
    gui_State.gui_Callback = str2func(varargin{1});
end

if nargout
    [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
    gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT

% --- Executes just before atoms_2_gui is made visible.
function atoms_2_gui_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
% varargin   command line arguments to atoms_2_gui (see VARARGIN)

% Choose default command line output for atoms_2_gui
handles.output = hObject;

% This sets up the initial plot - only do when we are invisible
% so window can get raised using atoms_2_gui.
% Put intialisation stuff in here (i.e. what to display when GUI first opens).
if strcmp(get(hObject,'Visible'),'off')
    handles.ROI = [1 1392 1 1040];
    handles.autoROI = get(handles.checkbox_auto_roi,'Value');
    handles.dpath =  [FindLastMeasurement() '\run_0.h5'];
    handles.do1DFit = get(handles.checkbox_1d_fit,'Value');
    handles.fitFunction = get(handles.popupmenu_choose_fitting_function,'Value');
    handles.do2DFit = get(handles.checkbox_2d_fit,'Value');
    handles.useBackground = get(handles.checkbox_use_background,'Value');
    handles.scaleBackground = get(handles.checkbox_scale_background,'Value');
    handles.saveOD = get(handles.checkbox_save_od,'Value');
end

if ischar(varargin{4}) % used by LabVIEW to pass the name of a new measurement.
    handles.dpath = varargin{4};
else
    error('The path must be a string')
end

if exist('varargin{5}','var')
    
end
% Update handles structure
guidata(hObject, handles);
% Update GUI
atoms_2_gui('NewImage_Callback',handles.figure1,[],handles.dpath);


% --- Outputs from this function are returned to the command line.
function varargout = atoms_2_gui_OutputFcn(hObject, eventdata, handles)
% varargout  cell array for returning output args (see VARARGOUT);
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Get default command line output from handles structure
varargout{1} = handles.output;


function NewImage_Callback(hObject,eventdata,dpath)
handles = guidata(hObject);
handles.dpath = dpath;
guidata(hObject,handles);
ProcessImageGUI(handles);


% --------------------------------------------------------------------
function FileMenu_Callback(hObject, eventdata, handles)
% hObject    handle to FileMenu (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)


% --------------------------------------------------------------------
function OpenMenuItem_Callback(hObject, eventdata, handles)
% hObject    handle to OpenMenuItem (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
[file,path] = uigetfile('*.h5');
% if ~isequal(file, 0)
%     open(file);
% end
atoms_2_gui('NewImage_Callback',handles.figure1,[],[path file]);

% --------------------------------------------------------------------
function PrintMenuItem_Callback(hObject, eventdata, handles)
% hObject    handle to PrintMenuItem (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
printdlg(handles.figure1)

% --------------------------------------------------------------------
function CloseMenuItem_Callback(hObject, eventdata, handles)
% hObject    handle to CloseMenuItem (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
selection = questdlg(['Close ' get(handles.figure1,'Name') '?'],...
    ['Close ' get(handles.figure1,'Name') '...'],...
    'Yes','No','Yes');
if strcmp(selection,'No')
    return;
end

delete(handles.figure1)


% --- Executes on button press in checkbox_auto_roi.
function checkbox_auto_roi_Callback(hObject, eventdata, handles)
% hObject    handle to checkbox_auto_roi (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
handles = guidata(hObject);
handles.autoROI = get(hObject,'Value');
guidata(hObject,handles);
atoms_2_gui('NewImage_Callback',handles.figure1,[],handles.dpath);
% Hint: get(hObject,'Value') returns toggle state of checkbox_auto_roi


% --- Executes on button press in pushbutton_choose_roi.
function pushbutton_choose_roi_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton_choose_roi (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
axes(handles.axes_processed);
[x,y] = ginput(2);
h = hObject;
handles = guidata(h);
handles.ROI = round([x' y']);
guidata(h,handles);
atoms_2_gui('NewImage_Callback',handles.figure1,[],handles.dpath);


% --- Executes during object creation, after setting all properties.
function text_numAtoms_CreateFcn(hObject, eventdata, handles)
% hObject    handle to text_numAtoms (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called


% % --- Executes when selected object is changed in img_config_radio_buttons.
% function img_config_radio_buttons_SelectionChangeFcn(hObject, eventdata, handles)
% % hObject    handle to the selected object in img_config_radio_buttons
% % eventdata  structure with the following fields (see UIBUTTONGROUP)
% %	EventName: string 'SelectionChanged' (read only)
% %	OldValue: handle of the previously selected object or empty if none was selected
% %	NewValue: handle of the currently selected object
% % handles    structure with handles and user data (see GUIDATA)
% tag = get(get(handles.img_config_radio_buttons,'SelectedObject'),'Tag')
% h = hObject;
% handles = guidata(h);
% switch tag % Get Tag of selected object.
%     case 'img_config_no_objective'
%         handles.img_config = 1;
%     case 'img_config_4X_objective'
%         handles.img_config = 2;
%     case 'img_config_10X_objective'
%         handles.img_config = 3;
%     otherwise
%         % Code for when there is no match.
% end
% guidata(h,handles);
% atoms_2_gui('NewImage_Callback',handles.figure1,[],handles.dpath);


% --- Executes on mouse press over axes background.
function axes_processed_ButtonDownFcn(hObject, eventdata, handles)
% hObject    handle to axes_processed (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)


% --- Executes on mouse press over axes background.
function axes_y_proj_ButtonDownFcn(hObject, eventdata, handles)
% hObject    handle to axes_y_proj (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)


% --- Executes on button press in pushbutton_reset_roi.
function pushbutton_reset_roi_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton_reset_roi (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
handles = guidata(hObject);
handles.ROI = [1 1392 1 1040];
guidata(hObject,handles);
atoms_2_gui('NewImage_Callback',handles.figure1,[],handles.dpath);


% --- Executes on selection change in popupmenu_choose_fitting_function.
function popupmenu_choose_fitting_function_Callback(hObject, eventdata, handles)
% hObject    handle to popupmenu_choose_fitting_function (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
handles = guidata(hObject);
handles.fitFunction = get(hObject,'Value');
guidata(hObject,handles);
atoms_2_gui('NewImage_Callback',handles.figure1,[],handles.dpath);
% Hints: contents = cellstr(get(hObject,'String')) returns popupmenu_choose_fitting_function contents as cell array
%        contents{get(hObject,'Value')} returns selected item from popupmenu_choose_fitting_function


% --- Executes during object creation, after setting all properties.
function popupmenu_choose_fitting_function_CreateFcn(hObject, eventdata, handles)
% hObject    handle to popupmenu_choose_fitting_function (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: popupmenu controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


% --- Executes on button press in checkbox_2d_fit.
function checkbox_2d_fit_Callback(hObject, eventdata, handles)
% hObject    handle to checkbox_2d_fit (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
handles = guidata(hObject);
handles.do2DFit = get(hObject,'Value');
guidata(hObject,handles);
atoms_2_gui('NewImage_Callback',handles.figure1,[],handles.dpath);
% Hint: get(hObject,'Value') returns toggle state of checkbox_2d_fit


% --- Executes on button press in checkbox_1d_fit.
function checkbox_1d_fit_Callback(hObject, eventdata, handles)
% hObject    handle to checkbox_1d_fit (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
handles = guidata(hObject);
handles.do1DFit = get(hObject,'Value');
guidata(hObject,handles);
atoms_2_gui('NewImage_Callback',handles.figure1,[],handles.dpath);
% Hint: get(hObject,'Value') returns toggle state of checkbox_1d_fit


% --- Executes on button press in checkbox_use_background.
function checkbox_use_background_Callback(hObject, eventdata, handles)
% hObject    handle to checkbox_use_background (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
handles = guidata(hObject);
handles.useBackground = get(hObject,'Value');
guidata(hObject,handles);
atoms_2_gui('NewImage_Callback',handles.figure1,[],handles.dpath);
% Hint: get(hObject,'Value') returns toggle state of checkbox_use_background


% --- Executes on button press in checkbox_scale_background.
function checkbox_scale_background_Callback(hObject, eventdata, handles)
% hObject    handle to checkbox_scale_background (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
handles = guidata(hObject);
handles.scaleBackground = get(hObject,'Value');
guidata(hObject,handles);
atoms_2_gui('NewImage_Callback',handles.figure1,[],handles.dpath);
% Hint: get(hObject,'Value') returns toggle state of checkbox_scale_background


% --- Executes on button press in checkbox_save_od.
function checkbox_save_od_Callback(hObject, eventdata, handles)
handles = guidata(hObject);
handles.saveOD = get(hObject,'Value');
guidata(hObject,handles);
% hObject    handle to checkbox_save_od (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hint: get(hObject,'Value') returns toggle state of checkbox_save_od
